<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { Dialog } from '@/components/Dialog'
import { ElButton, ElMessageBox } from 'element-plus'
import { Table } from '@/components/Table'
import { adminShopShopListeApi, adminShopShopExamineApi } from '@/api/cs/shop'
import { useTable } from '@/hooks/web/useTableTest'
import See from './components/See.vue'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { useIcon } from '@/hooks/web/useIcon'
import { getDictLabel } from '@/utils/lee'

// 获取字典
import { useCache } from '@/hooks/web/useCache'
const { wsCache } = useCache()
const dict: any = wsCache.get('dict')

// // 字典通过value获取label
// const getDictLabel = (type, value) => {
//   if (!dict[type]) {
//     alert(type + '字典不存在')
//   }
//   let data = dict[type].find((item) => {
//     return item.value == value
//   })
//   return data ? data.label : ''
// }

// const iconEdit = useIcon({ icon: 'ep:edit' })
const iconSee = useIcon({ icon: 'ep:view' })
// const iconDel = useIcon({ icon: 'ep:delete' })
const { register, tableObject, methods } = useTable<
  {
    total: number
    list: any[]
  },
  any
>({
  getListApi: adminShopShopListeApi,
  response: {
    data: 'data',
    count: 'count'
  }
})

const { getList, setSearchParams } = methods

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'shop_id',
    label: '主键',
    width: 60,
    search: {
      show: true
    }
  },
  {
    field: 'shop_name',
    label: '店铺名称',
    search: {
      show: true
    }
  },
  {
    field: 'shop_type',
    label: '店铺类型',
    width: 90,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return cellValue != '0' ? getDictLabel(dict, 'shop_shop_type', cellValue) : ' '
    },
    form: {
      component: 'Select',
      componentProps: {
        options: dict.shop_shop_type
      }
    },
    search: {
      component: 'Select',
      componentProps: {
        options: dict.shop_shop_type
      },
      show: true
    }
  },
  {
    field: 'surplus',
    label: '余额'
  },
  {
    field: 'shop_email',
    label: '店铺邮箱'
  },
  {
    field: 'customer_mobile',
    label: '客服电话'
  },
  {
    field: 'state',
    label: '状态',
    width: 90,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return cellValue != '0' ? getDictLabel(dict, 'shop_shop_state', cellValue) : ' '
    },
    form: {
      component: 'Select',
      componentProps: {
        options: dict.shop_shop_state
      }
    },
    search: {
      component: 'Select',
      componentProps: {
        options: dict.shop_shop_state
      },
      show: true
    }
  },
  {
    field: 'create_time',
    label: '创建时间',
    width: 180
  },
  {
    field: 'action',
    width: 300,
    label: '操作',
    form: {
      show: false
    },
    detail: {
      show: false
    }
  }
])

const { allSchemas } = useCrudSchemas(crudSchemas)

const dialogVisible = ref(false)

const dialogTitle = ref('')

const actionType = ref('')

const action = async (type: string, row: any = {}) => {
  actionType.value = type
  switch (type) {
    case 'see':
      dialogTitle.value = '查看'
      tableObject.currentRow = row
      dialogVisible.value = true
      break
    case 'examine':
      dialogTitle.value = '请审核这个商铺'
      tableObject.currentRow = row
      dialogVisible.value = true
      break
    default:
      console.log('未知类型')
  }
}

const examine = async (state) => {
  const confirmButtonText = state == 2 ? '同意' : '驳回'
  ElMessageBox.confirm('请确定', '审核', {
    confirmButtonText: '确认' + confirmButtonText,
    cancelButtonText: '关闭',
    type: 'warning'
  })
    .then(async () => {
      let res = await adminShopShopExamineApi({
        shop_id: tableObject.currentRow.shop_id,
        state: state
      })
        .catch(() => {})
        .finally(() => {})
      if (res) {
        dialogVisible.value = false
        tableObject.currentPage = 1
        getList()
      }
    })
    .catch(() => {})
}

/****************************** 初始化 start ******************************/

const init = async () => {
  // 先调店铺列表接口
  await getList()
}
init()

/****************************** 初始化 end ******************************/
</script>

<template>
  <ContentWrap>
    <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />

    <Table
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      :columns="allSchemas.tableColumns"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :border="true"
      :pagination="{
        total: tableObject.count
      }"
      @register="register"
    >
      <template #action="{ row }">
        <ElButton
          v-if="row.state != 1"
          type="success"
          :icon="iconSee"
          @click="action('see', row)"
          title="查看详情"
        >
          查看详情
        </ElButton>
        <ElButton v-if="row.state == 1" type="primary" @click="action('examine', row)" title="审核">
          审核店铺
        </ElButton>
      </template>
    </Table>
  </ContentWrap>

  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <See
      v-if="actionType == 'see' || actionType == 'examine'"
      :current-row="tableObject.currentRow"
    />
    <template #footer>
      <ElButton v-if="tableObject.currentRow.state == 1" type="primary" @click="examine(2)"
        >通过</ElButton
      >
      <ElButton v-if="tableObject.currentRow.state == 1" type="danger" @click="examine(3)"
        >驳回</ElButton
      >
      <ElButton @click="dialogVisible = false">关闭</ElButton>
    </template>
  </Dialog>
</template>
